package com.baosight.risk.web.risk;

import com.baosight.risk.api.riri.IRiaaRiskService;
import com.baosight.risk.base.controller.BaseController;
import com.baosight.risk.base.entity.MultipleUpdateStatus;
import com.baosight.risk.common.annotation.Log;
import com.baosight.risk.common.bean.BeanUtils;
import com.baosight.risk.common.enums.BusinessType;
import com.baosight.risk.common.result.AjaxResult;
import com.baosight.risk.common.utils.ExcelUtil;
import com.baosight.risk.common.utils.StringUtils;
import com.baosight.risk.service.riri.entity.RiaaRisk;
import com.baosight.risk.service.riri.vo.RiaaRiskVO;
import com.baosight.risk.service.riri.vo.RiriRiskVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import net.sf.json.JSONArray;
import org.apache.commons.collections.CollectionUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.ws.rs.core.MediaType;
import java.util.ArrayList;
import java.util.List;

/**
 * 类 名: RiaaRiskController
 * 作 者: 黎秋荣
 * 创 建：2019年05月07日
 * 版 本：v2.2.0
 * 历 史: (版本) 作者 时间 注释
 */
@Slf4j
@RestController
@RequestMapping("/risk/riaa")
@Api(value = "风险库", description = "风险库", produces = MediaType.APPLICATION_JSON)
public class RiaaRiskController extends BaseController {

    @Autowired
    private IRiaaRiskService riaaRiskService;

    /**
     * 根据ID查询跳转修改页面
     *
     * @param id
     * @return
     */
    @GetMapping("/edit")
    public AjaxResult edit(@RequestParam("id") String id) {
        return success(riaaRiskService.get(id));
    }

    /**
     * 根据ID查询跳转修改页面
     *
     * @param id
     * @return
     */
    @GetMapping("/merge")
    public AjaxResult merge(@RequestParam("id") String id) {
        return success(riaaRiskService.get(id));
    }

    /**
     * 查询风险库
     *
     * @param riaaRisk
     * @return
     */
    @RequestMapping("/list")
    public AjaxResult select(RiaaRiskVO riaaRisk) {
        log.info("查询风险库信息：" + riaaRisk.toString());
        return getResultPage(riaaRiskService.selectRiaaRisk(riaaRisk, getUser()));
    }

    /**
     * 根据IDS查询风险库
     *
     * @return
     */
    @GetMapping("/getByIds")
    @ApiOperation("根据IDS查询风险库")
    public AjaxResult getByIds(@RequestParam("ids") List<String> ids) {
        return getResultPage(riaaRiskService.getRiaaRiskByIds(ids));
    }


    /**
     * 保存风险库信息
     *
     * @param riaaRiskList
     * @return
     */
    @PostMapping("save")
    @Log(title = "保存风险库信息")
//    @RequiresPermissions("risk:riaa:add")
    public AjaxResult saveRiaaRisk(@RequestBody List<RiaaRiskVO> riaaRiskList) throws Exception {
        if (CollectionUtils.isEmpty(riaaRiskList)) {
            log.error("数据有误！");
            return error("数据有误！");
        }
        riaaRiskService.saveRiaaRisk(riaaRiskList, getUser());
        return toAjax(riaaRiskList.size());

    }

    /**
     * 提交风险库信息
     *
     * @param id
     * @return
     */
    @PostMapping("submit")
    @Log(title = "保存风险库信息")
//    @RequiresPermissions("risk:riaa:submit")
    public AjaxResult submit(String id) {
        List<String> ids = JSONArray.fromObject(id);
        List<RiaaRisk> risks = riaaRiskService.getRiaaRiskByIds(ids);
        if (ids.size() != risks.size()) {
            log.error("数据有误！");
            return error("数据有误！");
        }
        for (RiaaRisk risk : risks) {
            if (!"01".equals(risk.getStatus())) {
                log.error("状态有误！只有草稿状态才能提交！");
                return error("状态有误！只有草稿状态才能提交！");
            }
        }
        return toAjax(riaaRiskService.submitRisk(ids));
    }

    /**
     * 修改风险库信息
     *
     * @param riaaRisk 风险库信息
     * @return
     */
    @PostMapping("update")
    @Log(title = "修改风险库信息", businessType = BusinessType.UPDATE)
//    @RequiresPermissions("risk:riaa:edit")
    public AjaxResult update(RiaaRisk riaaRisk) {
        riaaRisk = (RiaaRisk) BeanUtils.updateBean(riaaRisk);
        log.info("修改风险库信息：" + riaaRisk.toString());
        return toAjax(riaaRiskService.updateRisk(riaaRisk));
    }

    /**
     * 禁用/启用
     *
     * @param multipleUpdateStatus
     * @return
     */
    @PostMapping("delete")
    @Log(title = "禁用风险库", businessType = BusinessType.DELETE)
//    @RequiresPermissions("risk:riaa:delete")
    public AjaxResult delete(@RequestBody MultipleUpdateStatus multipleUpdateStatus) {
        List<String> ids = multipleUpdateStatus.getIds();
        String status = multipleUpdateStatus.getStatus();
        List<RiaaRisk> risks = riaaRiskService.getRiaaRiskByIds(ids);
        if (ids.size() != risks.size()) {
            log.error("数据有误！");
            return error("数据有误！");
        }
        for (RiaaRisk risk : risks) {
            if ("01".equals(risk.getStatus()) || "02".equals(risk.getStatus())) {
                log.error("状态有误！草稿和提交状态不能适用此操作！");
                return error("状态有误！草稿和提交状态不能适用此操作！");
            }
        }
        return toAjax(riaaRiskService.deleteById(ids, status));
    }

    /**
     * 导出excel
     *
     * @return
     */
    @PostMapping("export")
    @ResponseBody
    @ApiOperation("导出风险点")
    public AjaxResult exportExcel(RiaaRiskVO riaaRisk) {
        log.info("导出风险点信息：" + riaaRisk.toString());
        ExcelUtil<RiaaRisk> excel = new ExcelUtil<>(RiaaRisk.class);
        List<RiaaRisk> list = riaaRiskService.selectRiaaRisk(riaaRisk, getUser());
        return excel.exportExcel(list, "风险库", riaaRisk.getColumNameList());
    }

    @PostMapping("updateRowOrder")
    @ApiOperation("排序")
    public AjaxResult updateRowOrder(@RequestBody RiaaRiskVO riaaRisk){
        riaaRiskService.updateRowOrder(riaaRisk, getUser());
        return success();
    }

}
